#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "cJSON.h"
#include <iostream>
#include <sstream>
#include <vector>
using namespace std;

int cJSON_Show(cJSON *obj, int n)
{
    cJSON *tmp;
    cJSON *p;
    int i, j;
    int arraySize;

    tmp = obj;
    if (!tmp)
        return -1;
    p = tmp;
    tmp = tmp->child;
    while (1) {
        if (!tmp) {
            if (p) {
                switch (p->type) {
                    case cJSON_Number:
                        printf("value:%d\n", p->valueint);
                        break;
                    case cJSON_String:
                        printf("value:%s\n", p->valuestring);
                        break;
                }
            }
            break;
        }

        if (tmp->type == cJSON_Array) {
            printf("array:%s, size:%d\n", tmp->string, cJSON_GetArraySize(tmp));
            arraySize = cJSON_GetArraySize(tmp);
            for (i = 0; i < arraySize; i++) {
                p = cJSON_GetArrayItem(tmp, i);
                    if (p) {
                        if (p->type == cJSON_Object)
                            cJSON_Show(p, n);
                        else {
                            cJSON_Show(p, n + 1);
                        }
                    } else {
                        printf("what?\n");
                    }
                }
            tmp = tmp->next;
            //cJSON_Show(tmp, n);       
        } else if (tmp->string && strcmp(tmp->string, "null")){
            printf("obj:%s\t", tmp->string);
            switch (tmp->type) {
                case cJSON_Number:
                    printf("value:%d\n", tmp->valueint);
                    break;
                case cJSON_String:
                    printf("value:%s\n", tmp->valuestring);
                    break;
                default:
                    printf("value:\n");
                    break;
            }
            tmp = tmp->next;
            //cJSON_Show(tmp, n);
        } else {
            switch (tmp->type) {
                case cJSON_Number:

                    printf("number:%lf", tmp->valuedouble);
                    break;
                case cJSON_String:
                    printf("string:%s\n", tmp->valuestring);
                    break;
            
            }
            tmp = tmp->next;
            //n++;
        }

    }

    return 0;
}

int cJSON_ChangeToTable(cJSON *obj, vector<string> &m_v, int &rowsize)
{
    cJSON *tmp;
    cJSON *pp, *cp, *p;
    int i, j;
    int arraySize;
	

    tmp = obj;
    if (!tmp)
        return -1;
    p = tmp;
    tmp = tmp->child;
    if (1) {
        if (!tmp) {
            return -1;
        }
		printf("string:%s\ntype:%d\n", tmp->string, tmp->type);
        if (tmp->type == cJSON_Array) {
			{
				printf("get theme\n");
				arraySize = cJSON_GetArraySize(tmp);
				for (i = 0, pp = cJSON_GetArrayItem(tmp, i);  i < 1; i++) {
					printf("string:%s, type:%d\n", pp->string, pp->type);
					if (pp->type != cJSON_Object)
						break;
					cp = pp->child;
					#if 1
					while (cp) {
						//cp = cp->child;
						//cp&&printf("%s ", cp->string);
						if (cp && cp->string && !(cp->type == cJSON_Array)) {
							//switch() (cp->type) 
							printf("%s ", cp->string);
							m_v.push_back(cp->string);
						}
						cp = cp->next;
					}
					#endif
				}
				//m_v.push_back(v_t);
				printf("\n");
				//break;
			}
			{
				printf("get value\n");
				rowsize = arraySize = cJSON_GetArraySize(tmp);
				rowsize += 1;
				for (i = 0;  i < arraySize; i++) {
					pp = cJSON_GetArrayItem(tmp, i);
					printf("string:%s, type:%d\n", pp->string, pp->type);
					if (pp->type != cJSON_Object)
						continue;
					cp = pp->child;
					#if 1
					//v_t.clear();
					while (cp) {
						stringstream ss;
						//cp = cp->child;
						//cp&&printf("%s ", cp->string);
						//printf("%s ", cp->string);
						if (cp && cp->string) {
							//switch() (cp->type) 
							printf("%s ", cp->string);
							switch (cp->type) {
								case cJSON_Number:
									printf("%d ", cp->valueint);
									ss.clear();
									ss << cp->valueint;
									m_v.push_back(ss.str());
									break;
								case cJSON_String:
									printf("%s ", cp->valuestring);
									m_v.push_back(string(cp->valuestring));
									break;
							}
							
						}
						printf("\n");
						cp = cp->next;
					}
					//m_v.push_back(v_t);
					#endif
				}
				return 0;
			}
        } else {	//	获取object
			{
				pp = tmp;
				printf("get theme\n");

				printf("string:%s, type:%d\n", pp->string, pp->type);
				cp = pp;
				#if 1
				while (cp) {
					//cp = cp->child;
					//cp&&printf("%s ", cp->string);
					if (cp && cp->string && !(cp->type == cJSON_Array)) {
						//switch() (cp->type) 
						printf("%s ", cp->string);
						m_v.push_back(cp->string);
					} else {
						break;
					}
					cp = cp->next;
				}
				#endif
				//m_v.push_back(v_t);
				printf("\n");
				//break;
			}
			{
				printf("get value\n");
				rowsize = 1;
				rowsize += 1;
				pp = tmp;
				cp = pp;
				//v_t.clear();
				while (cp) {
					//cp = pp->child;
					stringstream ss;
					if (cp && cp->string && !(cp->type == cJSON_Array)) {
						printf("%s ", cp->string);
						switch (cp->type) {
							case cJSON_Number:
								printf("%d ", cp->valueint);
								ss.clear();
								ss << cp->valueint;
								m_v.push_back(ss.str());
								break;
							case cJSON_String:
								printf("%s ", cp->valuestring);
								m_v.push_back(string(cp->valuestring));
								break;
						}
					} else {
						break;
					}
					printf("\n");
					cp = cp->next;
				}
			}
		}
	}
    return 0;
}

int main(int argc, const char *argv[])
{
	vector<string> m_v;
	int rowsize, colsize;
       cJSON *mJson = cJSON_Parse("{\"refresh_interval\":60,"
                        "\"timeout\":12.5,"
                        "\"dynamic_loading\":["
                            "{"
                                "\"so_path\":\"plugins/User.so\", \"load\":false, \"version\":1,"
                                "\"cmd\":["
                                     "{\"cmd\":2001, \"class\":\"neb::CmdUserLogin\"},"
                                     "{\"cmd\":2003, \"class\":\"neb::CmdUserLogout\"}"
                                "],"
                                "\"module\":["
                                     "{\"path\":\"im/user/login\", \"class\":\"neb::ModuleLogin\"},"
                                     "{\"path\":\"im/user/logout\", \"class\":\"neb::ModuleLogout\"}"
                                "]"
                             "},"
                             "{"
                             "\"so_path\":\"plugins/ChatMsg.so\", \"load\":false, \"version\":1,"
                                 "\"cmd\":["
                                      "{\"cmd\":2001, \"class\":\"neb::CmdChat\"}"
                                 "],"
                             "\"module\":[]"
                             "}"
                        "]"
                    "}");
    //cJSON_Show(mJson, 0);
	printf("-----------------------------------\n");
    cJSON_ChangeToTable(mJson, m_v, rowsize);
		printf("-----------------------------------\n");

	printf("m_v:%d\n", m_v.size());
	//vector<string> v;
	colsize = m_v.size()/rowsize;
	printf("colsize:%d\n", colsize);
	for (int i = 0;i < m_v.size(); i++)
	{
		if (!((i)%colsize))
			printf("\n");
		cout.setf(ios::left); 
		cout.width(15);
		cout << m_v[i].data();
		cout.unsetf(ios::left);
	}
	printf("\n");
	return 0;
}
